package org.emet.service;

import java.util.Date;
import java.util.List;

import org.emet.Constants;
import org.emet.dao.CarriageDao;
import org.emet.dao.EmployeeDao;
import org.emet.dao.VehicleDao;
import org.emet.entity.biz.Carriage;
import org.emet.entity.biz.Employee;
import org.emet.entity.biz.Vehicle;
import org.emet.jee.BusinessException;
import org.emet.jee.core.dao.Page;
import org.emet.service.dto.CarriageCriteria;
import org.emet.service.dto.CarriageStatisticsDisplayItem;
import org.hibernate.ObjectNotFoundException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.flex.remoting.RemotingDestination;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
@RemotingDestination
public class CarriageService {

	@Autowired
	private CarriageDao transportDao;
	@Autowired
	private EmployeeDao employeeDao;
	@Autowired
	private VehicleDao vehicleDao;

	private static Logger logger = LoggerFactory.getLogger(CarriageService.class);

	public List<Employee> getValidDrivers() {
		//		return employeeDao.getValidDrivers();
		return employeeDao.getAll();
	}

	public List<Employee> getValidGathers() {
		//		return employeeDao.getValidGathers();
		return employeeDao.getAll();
	}

	public List<Vehicle> getValidVehicles() {
		return vehicleDao.getValidVehicles();
	}

	public List<Employee> getAllDrivers() {
		//return employeeDao.findBy("title", Constants.DRIVER);
		return employeeDao.getAll();
	}

	public List<Employee> getAllGathers() {
		//		return employeeDao.findBy("title", Constants.GATHER);//
		return employeeDao.getAll();
	}

	public List<Vehicle> getAllVehicles() {
		return vehicleDao.getAll();
	}

	public Long save(Carriage transport) {
		if (transport.getId() != null) {
			Carriage t = transportDao.get(transport.getId());
			BeanUtils.copyProperties(transport, t, new String[] { "timeStamp" });
			setVehsOrdered(t, t.getVehLPN());
			return t.getId();
		} else {
			try {
				setVehsOrdered(transport, transport.getVehLPN());
				transportDao.save(transport);
				return transport.getId();
			} catch (Exception e) {
				logger.error(e.getMessage(), e);
				throw new BusinessException("发生异常!");
			}
		}
	}

	private void setVehsOrdered(Carriage transport, String lpn) {
		Vehicle veh = vehicleDao.getByLpn(lpn);
		if (veh.getStatus().equals(Constants.STATUS_OK)) {
			veh.setStatus(Constants.STATUS_ORDERED);
		}
	}

	public Carriage get(Long id) {
		try {
			Carriage c = transportDao.get(id);
			Vehicle v = vehicleDao.getByLpn(c.getVehLPN());
			VehicleStatusUpdateHelper.change2BusyStatus(c, v);
			return c;
		} catch (ObjectNotFoundException e) {
			return null;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BusinessException("发生异常!");
		}
	}

	public void del(Long... ids) {
		for (Long id : ids) {
			if (notExist(id)) {
				continue;
			}

			try {
				transportDao.delete(id);
			} catch (Exception e) {
				logger.error(e.getMessage(), e);
				throw new BusinessException("发生异常!");
			}
		}
	}

	private boolean notExist(Long id) {
		return get(id) == null;
	}

	public Page<Carriage> search(Page<Carriage> page, CarriageCriteria transport) {
		try {
			if (transport == null) {
				transport = new CarriageCriteria();
			}
			transportDao.search(page, transport);

			VehicleStatusUpdateHelper.change2BusyStatus(page.getResult(), vehicleDao);
			return page;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BusinessException("发生异常!");
		}
	}

	//Report
	@Transactional(readOnly = true)
	public List<CarriageStatisticsDisplayItem> searchByYear(int startYear, int endYear, boolean groupByDriver) {
		try {
			return transportDao.searchByYear(startYear, endYear, groupByDriver);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BusinessException("发生异常!");
		}
	}

	@Transactional(readOnly = true)
	public List<CarriageStatisticsDisplayItem> searchByMon(int startYear, int startMon, int endYear, int endMon,
			boolean groupByDriver) {
		try {
			return transportDao.searchByMon(startYear, startMon, endYear, endMon, groupByDriver);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BusinessException("发生异常!");
		}
	}

	@Transactional(readOnly = true)
	public List<CarriageStatisticsDisplayItem> searchByDate(Date startDate, Date endDate, boolean groupByDriver) {
		try {
			return transportDao.searchByDate(startDate, endDate, groupByDriver);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BusinessException("发生异常!");
		}
	}

}
